import { filterOrder, getYuanGong, tradeListFields } from '@/utils/helper';
import { useEffect, useRef, useState } from 'react';
import { ProTable, ProColumns } from '@ant-design/pro-components';
import { getUsdtNum, isSuccess, setDefEmployeeId } from '@/utils';
import { tradeListApi, userListApi } from '@/api';
import dayjs from 'dayjs';
import { createTimeLie, yuanGongLie } from './ComonOrder';

export default function TradeList() {
  const [yuanGongs, setYuanGongs] = useState<any>([]);
  const tableRef: any = useRef('');
  const [defYuanGongId, setDefYuanGongId] = useState<any>('');

  useEffect(() => {
    initYuanGong();
  }, []);

  // 请求:成交记录
  async function request(e: any) {
    const _params: any = {
      ...e,
      contract: '',
      userIds: [e.yuanGongId],
    };
    const res: any = await tradeListApi(_params);
    // console.log('res: 成交记录: ', res.data);
    if (!isSuccess(res)) return { data: [], total: 0 };
    const arr = res?.data?.map((item: any, i: number) => ({ ...item, id: i + 1 }));
    // const _list = filterOrder(arr, _params);
    return { data: arr, total: res?.data?.total, success: true };
  }

  const columns: any[] = [
    {
      title: '合约',
      dataIndex: 'contract',
      hideInSearch: true,
    },
    createTimeLie(),
    yuanGongLie(yuanGongs, defYuanGongId, true),
    {
      title: '成交时间',
      dataIndex: 'create_time',
      hideInSearch: true,
      render: (text: any) => <>{dayjs(text * 1000).format('YYYY-MM-DD HH:mm:ss')}</>,
    },
    {
      title: '方向',
      dataIndex: 'newSize',
      hideInSearch: true,
      render: (text: any, record: any) => (
        <div className={`${text > 0 ? 'color-red' : 'color-green'}`}>
          {text > 0 ? '做多' : '做空'}
        </div>
      ),
    },
    {
      title: '委托ID',
      dataIndex: 'order_id',
      hideInSearch: true,
    },
    {
      title: '数量(USDT)',
      dataIndex: 'size',
      hideInSearch: true,
      render: (text: any, record: any) => <div>{getUsdtNum(record)}</div>,
    },
    {
      title: '成交均价',
      dataIndex: 'price',
      hideInSearch: true,
    },
    {
      title: '角色',
      dataIndex: 'role',
      hideInSearch: true,
    },
    {
      title: '手续费',
      dataIndex: 'fee',
      render: (text: any) => <div>{text} USDT</div>,
      hideInSearch: true,
    },
  ];

  // 员工列表
  const initYuanGong = async () => {
    const yuanGongs: any = await getYuanGong(userListApi);
    setYuanGongs(yuanGongs);
    setDefYuanGongId(setDefEmployeeId(yuanGongs));
  };

  return (
    <>
      {yuanGongs && Object.keys(yuanGongs).length > 0 && (
        <ProTable
          rowKey="id"
          columns={columns}
          request={request}
          actionRef={tableRef}
          options={false}
          scroll={{ x: 1900 }}
          // pagination={false}
        />
      )}
    </>
  );
}
